package model;

import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.properties.EncryptableProperties;

public class LogModel {
	private EncryptableProperties configFile;
	private String sLanguage;
	private Locale locale;

	private Connection cConnection;
	private ResultSet rResultSet;
	private Statement sStatement;
	private String[][] logs;
	private String dbURL;
	private String username;
	private String password;

	public LogModel() {
		configFile = loadConfiguration();
		cConnection = null;
		sStatement = null;
		rResultSet = null;
		dbURL = configFile.getProperty("databaseURL");
		username = configFile.getProperty("databaseUser");
		password = configFile.getProperty("databasePassword");
	}

	public String[][] readLogs() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			cConnection = DriverManager.getConnection(dbURL, username, password);
			sStatement = cConnection.createStatement();

			if(sStatement.execute("SELECT COUNT(*) FROM logs")) {
				rResultSet = sStatement.getResultSet();
				rResultSet.next();
			} else {
				System.err.println("select count failed");
			}
			int iLogs = rResultSet.getInt(1);

			logs = new String[iLogs][4];
			if (sStatement.execute("SELECT quan, tipus, qui, que FROM logs")) {
				rResultSet = sStatement.getResultSet();
			} else {
				System.err.println("select failed");
			}

			int i = 0;
			while (rResultSet.next()) {
				logs[i][0] = rResultSet.getString("quan");
				logs[i][1] = rResultSet.getString("tipus");
				logs[i][2] = rResultSet.getString("qui");
				logs[i][3] = rResultSet.getString("que");
				i++;
			}

		} catch (ClassNotFoundException ex) {
			System.err.println("Failed to load mysql driver");
			System.err.println(ex);
		} catch (SQLException ex) {
			System.out.println("SQLException: " + ex.getMessage()); 
			System.out.println("SQLState: " + ex.getSQLState()); 
			System.out.println("VendorError: " + ex.getErrorCode()); 
		} finally {
			if (rResultSet != null) {
				try {
					rResultSet.close();
				} catch (SQLException ex) { /* ignore */ }
				rResultSet = null;
			}
			if (sStatement != null) {
				try {
					sStatement.close();
				} catch (SQLException ex) { /* ignore */ }
				sStatement = null;
			}
			if (cConnection != null) {
				try {
					cConnection.close();
				} catch (SQLException ex) { /* ignore */ }
				cConnection = null;
			}
		}
		return logs;
	}

	public void setLogs(String[][] logs) {
		this.logs = logs;
	}

	public String[][] getLogs() {
		return logs;
	}

	public void setLanguage(String sLanguage) {
		configFile.setProperty("defaultLanguage", sLanguage);
		try {
			configFile.store(new FileOutputStream(new File("openlog.properties")), "OpenLogViewer configuration file");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void open(URI uri) {
		if (Desktop.isDesktopSupported()) {
			Desktop desktop = Desktop.getDesktop();
			try {
				desktop.browse(uri);
			} catch (IOException e) {
			}
		} else {
		}
	}

	public EncryptableProperties loadConfiguration() {
		try {
			StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
			encryptor.setPassword("0p3nL0gV|w3r&");
			configFile = new EncryptableProperties(encryptor);
			configFile.load(new FileInputStream("openlog.properties"));
			sLanguage = configFile.getProperty("defaultLanguage");
		}
		catch (FileNotFoundException e) { e.printStackTrace(); }
		catch (IOException e) { e.printStackTrace(); }

		return configFile;
	}

	public String getLanguage() {
		return sLanguage;
	}

	public Properties getConfigFile() {
		return this.configFile;
	}

	public ResourceBundle getLabels() {
		Locale.setDefault(new Locale(sLanguage));
		locale = Locale.getDefault();
		return (ResourceBundle.getBundle("labels/Labels", locale));
	}
	
	
	public void exportPDF(String[][] logs, HashMap<String, String> exportParams) {
		Exporter exporter = new Exporter();
		exporter.exportPDF(logs, exportParams);
	}
}
